package sitesettinghandler

import (
	encodingXml "encoding/xml"
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
	"yundian/internel/app/helper"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	. "yundian/internel/pkg/dingo/result"
	"yundian/internel/pkg/wechatsmart"
)

//设置批量导入
func HandlerBatchImportSkill(c *gin.Context) {
	param := wechatsmart.ImportSkillReq{}
	dingo.ErrorIf(c.Bind(&param)).Unwrap()

	encrypt := &wechatsmart.BatchImportSkillRes{}
	skill := wechatsmart.Skill{}
	//	相似问题
	var question []wechatsmart.CDATA
	var questionData wechatsmart.CDATA

	for _, v := range param.Skill {
		for _, paramValue := range v.Question {
			questionData.Value = paramValue
			question = append(question, questionData)
		}

		//机器人回答
		var answer []wechatsmart.CDATA
		var answerData wechatsmart.CDATA
		for _, paramValue := range v.Answer {
			answerData.Value = paramValue
			answer = append(answer, answerData)
		}

		encrypt.ManagerId.Value = global.Cfg.Smart.SmartManagerId

		skill.Title.Value = v.Title
		skill.SkillName.Value = v.SkillName
		skill.Question = question
		skill.Answer = answer

	}

	encrypt.Skill = append(encrypt.Skill, skill)

	dataByte, _ := encodingXml.Marshal(encrypt)
	fmt.Println(string(dataByte))

	result, err := wechatsmart.RobotSetting(string(dataByte), "batchimportskill")

	if err != nil {
		Response(c)("导入失败", CodeError, nil)(Error)
	}

	Response(c)("导入成功", CodeInsertSuccess, result)(OK)
	return
}

//机器人发布
func HandlerRobotRelease(c *gin.Context) {
	param := &wechatsmart.RobotRelease{}
	param.ManagerId.Value = global.Cfg.Smart.SmartManagerId
	dataByte, _ := encodingXml.Marshal(param)
	result, err := wechatsmart.RobotSetting(string(dataByte), "publish")

	if err != nil {
		Response(c)("发布失败", CodeError, nil)(Error)
	}

	Response(c)("发布成功", CodeInsertSuccess, result)(OK)
	return
}

//机器人发布进度查询
func HandlerRobotPublishProgress(c *gin.Context) {
	param := &wechatsmart.RobotRelease{}
	param.ManagerId.Value = global.Cfg.Smart.SmartManagerId
	dataByte, _ := encodingXml.Marshal(param)
	result, err := wechatsmart.RobotSetting(string(dataByte), "publish_progress")

	if err != nil {
		Response(c)("查询失败", CodeError, nil)(Error)
	}

	Response(c)("查询成功", CodeInsertSuccess, result)(OK)
	return
}

//设置自动回复
func HandlerSetAutoReply(c *gin.Context) {
	param := &wechatsmart.SetAutoReplyReq{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()

	param.ManagerId = global.Cfg.Smart.SmartManagerId

	setAutoReplyXml := &wechatsmart.SetAutoReplyRes{}

	//要设置的相似问题
	var question wechatsmart.Question
	var data wechatsmart.CDATA
	for _, paramValue := range param.List {
		data.Value = paramValue
		question.Question = append(question.Question, data)
	}

	setAutoReplyXml.ManagerId.Value = global.Cfg.Smart.SmartManagerId
	setAutoReplyXml.Title.Value = param.Title
	setAutoReplyXml.SkillName.Value = param.SkillName
	setAutoReplyXml.Content.Value = param.Content
	setAutoReplyXml.List = question

	dataByte, _ := encodingXml.Marshal(setAutoReplyXml)

	result, err := wechatsmart.RobotSetting(string(dataByte), "setautoreply")

	if err != nil {
		Response(c)("设置失败", CodeError, nil)(Error)
	}

	Response(c)("设置成功", CodeInsertSuccess, result)(OK)
	return
}

//批量设置用户标签
func HandlerSetLabelBatch(c *gin.Context) {
	param := &wechatsmart.SetLabelBatchReq{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()

	param.AppId = global.Cfg.Smart.SmartAppId
	setLabelBatchXml := &wechatsmart.SetLabelBatchRes{
		AppId:    param.AppId,
		Category: param.Category,
		Label:    param.Label,
		Desc:     param.Desc,
		List:     param.List,
	}

	dataByte, _ := encodingXml.Marshal(setLabelBatchXml)
	result, err := wechatsmart.UserLabelBatchSet(string(dataByte))

	if err != nil {
		Response(c)("设置失败", CodeError, nil)(Error)
	}

	Response(c)("设置成功", CodeInsertSuccess, result)(OK)
	return
}

//智能对话
func HandlerSmartDialogue(c *gin.Context) {
	param := &wechatsmart.SmartDialogueReq{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	//userId, err := helper.GetUserIdFromCtx(c)
	//if err != nil {
	//	Response(c)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
	//	return
	//}

	result, err := wechatsmart.SmartDialogue(param.Query, "125254as")
	if err != nil {
		Response(c)("", CodeError, nil)(Error)
	}

	Response(c)("", CodeInsertSuccess, result)(OK)
	return
}

//获取热问
func HandlerGetHotQueryList(c *gin.Context) {
	param := &wechatsmart.HotQueryListReq{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()

	param.Date = time.Now().Format("2006-01-02")

	hotQueryXML := wechatsmart.HotQueryListRes{}

	xmlByte, _ := encodingXml.MarshalIndent(&param, "", "  ")
	encodingXml.Unmarshal(xmlByte, &hotQueryXML)

	dataByte, _ := encodingXml.Marshal(hotQueryXML)
	result, err := wechatsmart.GetHotQueryList(string(dataByte))

	if err != nil {
		Response(c)("设置失败", CodeError, nil)(Error)
	}

	Response(c)("设置成功", CodeInsertSuccess, result)(OK)
	return
}

//发送客服消息
func HandlerSendCustomerServiceMsg(c *gin.Context) {
	param := &wechatsmart.SendCustomerServiceMsg{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()

	openId, err := helper.GetUserNameFromCtx(c)
	if err != nil {
		Response(c)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
		return
	}
	param.Openid = openId

	replyMsg := &wechatsmart.SendCustomerServiceMsg{
		AppId:      param.AppId,
		Openid:     param.Openid,
		Msg:        param.Msg,
		Channel:    param.Channel,
		KeFuName:   param.KeFuName,
		KeFuAvatar: param.KeFuAvatar,
	}
	dataByte, err := encodingXml.Marshal(replyMsg)
	result, err := wechatsmart.RobotSetting(string(dataByte), "sendmsg")

	if err != nil {
		Response(c)("查询失败", CodeError, nil)(Error)
	}

	Response(c)("查询成功", CodeInsertSuccess, result)(OK)
	return
}
